JavaScript syntax
part 42/59 · 107.4 KB total
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
• break; is optional; however, it is usually needed, since otherwise code execution will continue to the body of the next case block. This fall through behavior can be used when the same set of statements apply in several cases, effectively creating a disjunction between those cases.
• Add a break statement to the end of the last case as a precautionary measure, in case additional cases are added later.
• String literal values can also be used for the case values.
• Expressions can be used instead of values.
• The default case (optional) is executed when the expression does not match any other specified cases.
• Braces are required.
For loop
The syntax of the JavaScript for loop is as follows:
for (initial; condition; loop statement) {
/*
statements will be executed every time
the for{} loop cycles, while the
condition is satisfied
*/
}
or
for (initial; condition; loop statement(iteration)) // one statement
For ... in loop
The syntax of the JavaScript for ... in loop is as follows:
for (var property_name in some_object) {
// statements using some_object[property_name];
}
• Iterates through all enumerable properties of an object.
• Iterates through all used indices of array including all user-defined properties of array object, if any. Thus it may be better to use a traditional for loop with a numeric index when iterating over arrays.
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────